package com.example.blog.config;

import com.example.blog.controller.ArticleController;
import com.example.blog.model.ArticleInfo;
import com.example.blog.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.Async;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.PostConstruct;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @Question AppConfig
 * @Date 2023/1/7 11:04
 * @Solution
 */
@Configuration
public class AppConfig implements WebMvcConfigurer {
    @Autowired
    LoginInterceptor loginInterceptor;
    @Autowired
    ArticleService articleService;
    @Autowired
    ArticleController articleController;
    //开启线程一直检查是否有博文定时时间到了
    @PostConstruct
    public void star() {
        startCheck();
    }
    @Async
    public void startCheck() {
        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                while(true) {
                    //获取所有文章
                    List<ArticleInfo> list = articleService.getAll();
                    //遍历检查state为-1的文章
                    for (ArticleInfo articleInfo : list) {
                        if (articleInfo.getState() == -1) {
                            //检查定时发布的时间是否到了
                            String DateStr = articleInfo.getCreatetime();
                            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd kk:mm:ss");
                            try {
                                Date setDate = dateFormat.parse(DateStr);
                                Date now = new Date();
                                if (now.compareTo(setDate) >= 0) {//小于等于0为刚好等于发布日期或者小于发布日期
                                    //调用revise 将state改为1 发布
                                    System.out.println("文章id:"+articleInfo.getId()+" 已经于"+articleInfo.getCreatetime()+"定时发布");
                                    articleController.revise(articleInfo.getId(), 1, articleInfo.getTitle(), articleInfo.getContent());
                                }
                            } catch (ParseException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                }
            }
        });
        thread.start();
    }
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(loginInterceptor)
                .addPathPatterns("/**")
                .excludePathPatterns("/**/*.html")
                .excludePathPatterns("/**/*.css")
                .excludePathPatterns("/**/*.js")
                .excludePathPatterns("/**/*.jpg")
                .excludePathPatterns("/**/*.png")
                .excludePathPatterns("/**/*.md")
                .excludePathPatterns("/**/*.svg")
                .excludePathPatterns("/**/*.eot")
                .excludePathPatterns("/**/*.woff")
                .excludePathPatterns("/**/*.otf")
                .excludePathPatterns("/**/*.ttf")
                .excludePathPatterns("/**/*.woff2")
                .excludePathPatterns("/**/*.gif")
                .excludePathPatterns("/**/*.ico")
                .excludePathPatterns("/**/*.json")
                .excludePathPatterns("/**/*.scss")
                .excludePathPatterns("/user/login")
                .excludePathPatterns("/user/reg")
                .excludePathPatterns("/**/*.img")
                .excludePathPatterns("/user/code")
                .excludePathPatterns("/user/checke")
                .excludePathPatterns("/user/sendmail")
                .excludePathPatterns("/user/revise")
                .excludePathPatterns("/comment/getcom")
                .excludePathPatterns("/comment/getname");
    }
}
